#!/usr/bin/env bash

set -e

"$VIRTUAL_ENV/bin/pip" install -e .
"$VIRTUAL_ENV/bin/gitfs" "$BARE_REPO" "$MNT_DIR" -o \
"repo_path=$REPO_DIR,fetch_timeout=.1,merge_timeout=.1,\
allow_other=true,foreground=true,log=log.txt,debug=true,idle_fetch_timeout=.1" &\

echo $! > "$GITFS_PID"

export REPO_NAME="$REPO_NAME"
export MOUNT_PATH="$MNT_DIR"
export REPO_PATH="$REPO_DIR"
export REMOTE="$REMOTE"

"$VIRTUAL_ENV/bin/py.test" --assert=plain --cov --cov-config .coveragerc "$TESTS"
kill -9 "$(cat "$GITFS_PID")"

if [ "$EUID" -ne 0 ]; then
    sudo umount -f "$MNT_DIR"
else
    umount -f "$MNT_DIR"
fi

rm -rf "$TEST_DIR"
